home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / opendir.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  2KB  |  99 lines

  1. /* opendir routine */
  2.  
  3. /* under MiNT (v0.9 or better) these use the appropriate system calls.
  4.  * under TOS or older versions of MiNT, they use Fsfirst/Fsnext
  5.  *
  6.  * Written by Eric R. Smith and placed in the public domain
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <types.h>
  12. #include <limits.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include <osbind.h>
  16. #include <mintbind.h>
  17. #include "lib.h"
  18.  
  19. extern int __mint;
  20. extern ino_t __inode;    /* in stat.c */
  21.  
  22. DIR *
  23. opendir(uname)
  24.     const char *uname;
  25. {
  26.     DIR *d;
  27.     long r;
  28.     _DTA *olddta;
  29.     char name[PATH_MAX];
  30.     char dirpath[PATH_MAX];
  31.     char *p;
  32.  
  33.     d = malloc(sizeof(DIR));
  34.     if (!d) {
  35.         errno = ENOMEM;
  36.         return d;
  37.     }
  38.  
  39.     _unx2dos(uname, name);
  40.  
  41.     if (__mint > 8) {
  42.         r = Dopendir(name, 0);
  43.         if ( (r & 0xff000000L) == 0xff000000L ) {
  44.             errno = (int) -r;
  45.             free(d);
  46.             return 0;
  47.         }
  48.         d->handle = r;
  49.         d->buf.d_off = 0;
  50.         return d;
  51.     }
  52.  
  53. /* TOS emulation routines */
  54.  
  55.     p = name;
  56.     if (p) {
  57.     /* find the end of the string */
  58.         for (p = name; *p; p++) ;
  59.  
  60.     /* make sure the string ends in '\' */
  61.         if (*(p-1) != '\\') {
  62.             *p++ = '\\';
  63.         }
  64.     }
  65.  
  66.     strcpy(p, "*.*");
  67.     olddta = Fgetdta();
  68.     Fsetdta(&(d->dta));
  69.     r = Fsfirst(name, 0x17);
  70.     Fsetdta(olddta);
  71.  
  72.     if (r == 0) {
  73.         d->status = _STARTSEARCH;
  74.     } else if (r == -ENOENT) {
  75.         d->status = _NMFILE;
  76.     } else {
  77.         free(d);
  78.         errno = (int) -r;
  79.         return 0;
  80.     }
  81.     d->buf.d_off = 0;
  82. /* for rewinddir: if necessary, build a relative path */
  83.     if (name[1] == ':') {    /* absolute path, no problem */
  84.         dirpath[0] = 0;
  85.     } else {
  86.         dirpath[0] = Dgetdrv() + 'A';
  87.         dirpath[1] = ':';
  88.         dirpath[2] = 0;
  89.         if (*name != '\\')
  90.             (void)Dgetpath(dirpath+2, 0);
  91.     }
  92.     d->dirname = malloc(strlen(dirpath)+strlen(name)+1);
  93.     if (d->dirname) {
  94.         strcpy(d->dirname, dirpath);
  95.         strcat(d->dirname, name);
  96.     }
  97.     return d;
  98. }
  99.